var oprateUrl = DOMAIN + '/v1/ticket' + isLogin;
function loadTable() {
	$("#table").bootstrapTable({ // 对应table标签的id
		url: oprateUrl,
		method: 'get',
		cache: false,
		striped: true,
		pagination: true,
		pageSize: 5,
		pageNumber: 1,
		pageList: [5, 10],
		search: false,
		showColumns: false,
		clickToSelect: true,
		queryParams: {
			deleteFlg: 0
		},
		smartDisplay: false,
		columns: [ {
			field: "id",
			title: "ID",
			align: "center",
			valign: "middle",
			sortable: "true",
			visible: false
		}, {
			field: "ticketNo",
			title: "申请单号",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "ticketDate",
			title: "申请日期",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "proposer",
			title: "申请人",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "sumMoney",
			title: "总金额",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "comments",
			title: "备注",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "ticketStatus",
			title: "开票申请状态",
			align: "center",
			valign: "middle",
			sortable: "true"
		},{
			field: "",
			title: "操作",
			align: "center",
			valign: "middle",
			events: funOprate,
			formatter: function (value, row, index) {
				if (row.ticketStatus != "审核中") {
					return '<button ticketNo="' + row.ticketNo + '" type="button" data-toggle="modal" data-edit="myModal" data-target="#myModal" onclick="EditInfo(this)" class="btn btn-primary btn-sm edit">&emsp;查看</button>';
				} else {
					return '<button ticketNo="' + row.ticketNo + '" type="button" data-toggle="modal" data-edit="myModal" data-target="#myModal" onclick="AuditInfo(this)" class="btn btn-primary btn-sm edit">&emsp;审核</button>';
				}
			}
		}],
		//>>>>>>>>>>>>>>导出excel表格设置
		showExport: true,
		showExport: true,
		//是否显示导出按钮
		buttonsAlign: "left",
		//按钮位置
		exportDataType: 'all',
		//导出的方式 all全部 selected已选择的  basic', 'all', 'selected'.
		Icons: 'glyphicon glyphicon-export',
		exportTypes: ['excel', 'doc', 'xlsx', 'txt',],
		//导出文件类型
		exportOptions: {
			ignoreColumn: [0, 0],  //忽略某一列的索引，id删除更新用用，不显示与导出

			fileName: '导出数据表',
			//文件名称设置
			worksheetName: 'sheet1',
			//表格工作区名称
			tableName: '导出数据表',
			// excelstyles: ['background-color', 'color', 'font-size', 'font-weight'], 设置格式

		},
		//导出excel表格设置<<<<<<<<<<<<<<<<
		formatNoMatches: function () {
			return '没有找到匹配的记录';
		}
	});
	function queryParams(params) {
		var params = {};
		return params;
	};
}

//查询
$("#selectPurchaseBtn").click(function () {
	//新查询参数
	function queryParamNew(params) {
		var params = {};
		params.beginDate = $("#beginDate").val();
		params.endDate = $("#endDate").val();
		params.ticketNo = $("#ticketNo").val();
		params.ticketStatus = $("#ticketStatus option:checked").val();
		return params;
	};
	var url = DOMAIN + '/v1/ticket' + isLogin;
	$('#table').bootstrapTable(
		'refreshOptions',
		{
			url: url,
			//查询参数
			queryParams: queryParamNew
		}
	);
})

$("#tableAddDetail").bootstrapTable({ // 对应table标签的id
	url: "",
	method: 'get',
	cache: false,
	striped: true,
	pagination: true,
	pageSize: 5,
	pageNumber: 1,
	pageList: [5, 10],
	search: false,
	showColumns: false,
	clickToSelect: true,
	queryParams: queryParams,
	smartDisplay: false,
	columns: [{
		checkbox: true
	},
	{
		field: "id",
		title: "ID",
		align: "center",
		valign: "middle",
		sortable: "true",
		visible: false
	},
	{
		field: "pid",
		title: "序号",
		align: "center",
		valign: "middle",
		sortable: "true",
		visible: false
	}, {
		field: "ticketNo",
		title: "申请单号",
		align: "center",
		valign: "middle",
		sortable: "true",
		visible: false
	}, {
		field: "productLevelOne",
		title: "产品一级类型",
		align: "center",
		valign: "middle",
		sortable: "true"
	}, {
		field: "productLevelTwo",
		title: "产品二级类型",
		align: "center",
		valign: "middle",
		sortable: "true",
		formatter: function (value, row, index) {
                return row.productLevelTwo==""?"-":row.productLevelOne;
            }
	}, {
		field: "productName",
		title: "产品名称",
		align: "center",
		valign: "middle",
		sortable: "true",
		formatter: function (value, row, index) {
                return row.productName==""?"-":row.productName;
            }
	}, {
		field: "money",
		title: "金额",
		align: "center",
		valign: "middle",
		sortable: "true"
	}],
	formatNoMatches: function () {
		return '';
	}
});
function queryParams(params) {
	return params;
};



//查看信息
function EditInfo(e) {
	$('#tableAddDetail').bootstrapTable('removeAll');
	$("#Audit-btn").css("display","none");
	function queryParamNew(params) {
		var params = {};
		return params;
	};

	var ticketNo = $(e).attr('ticketNo');
	var url = DOMAIN + '/v1/ticketInfo' + isLogin + '&ticketNo=' + ticketNo;
	$('#tableAddDetail').bootstrapTable(
		'refresh',
		{
			url: url,
			//查询参数
			queryParams: queryParamNew
		}
	);
}

//审核信息
function AuditInfo(e){
	$('#tableAddDetail').bootstrapTable('removeAll');
	$("#Audit-btn").css("display","block");
	function queryParamNew(params) {
		var params = {};
		return params;
	};

	var ticketNo = $(e).attr('ticketNo');
	var url = DOMAIN + '/v1/ticketInfo' + isLogin + '&ticketNo=' + ticketNo;
	$('#tableAddDetail').bootstrapTable(
		'refresh',
		{
			url: url,
			//查询参数
			queryParams: queryParamNew
		}
	);
}

//下拉框填充
$(function () {
	//	时间初试化
	$('#beginDate').datetimepicker({
		minView: "month",
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		todayBtn: 1,
		autoclose: true
	});
	$('#endDate').datetimepicker({
		minView: "month",
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		todayBtn: 1,
		autoclose: true
	});
	time_rota2($("#beginDate"));
	time_rota($("#endDate"));
	
	loadTable();
	//新查询参数
	function queryParamNew(params) {
		var params = {};
		params.beginDate = $("#beginDate").val();
		params.endDate = $("#endDate").val();
		params.ticketNo = $("#ticketNo").val();
		params.ticketStatus = $("#ticketStatus option:checked").val();
		return params;
	};
	var url = DOMAIN + '/v1/ticket' + isLogin;
	$('#table').bootstrapTable(
		'refreshOptions',
		{
			url: url,
			//查询参数
			queryParams: queryParamNew
		}
	);
})

//关闭
$("#AddPur-closeBtn").click(function () {
	$("#close-btn")[0].click();
})

$("#Audit-pass").on("click",function(){
	var rf1 = confirm("确定通过此申请吗？");
	if(rf1){
		var params = getDataForAdd();
		params.ticketStatus = "审核通过";
		updateStatus(params);
	}
});

$("#Audit-noPass").on("click",function(){
	var rf2 = confirm("确定不通过此申请吗？");
	if(rf2){
		var params = getDataForAdd();
		params.ticketStatus = "审核未通过";
		updateStatus(params);
	}
});

function updateStatus(params){
	params = JSON.stringify(params);
	var mainId = $("#mainId").val();
	$.ajax({
			type: "patch",
			url: DOMAIN + '/v1/ticket/' + mainId + isLogin,
			data: params,
			dataType: 'JSON',
			contentType: "application/JSON; charset=utf-8",
			success: function (result) {
				if (result.status == 200) {
					alert("操作成功!");
					$("#table").bootstrapTable("refresh", {
						silent: true //静态刷新
					});
				}
			},
			error: function () {
				alert('操作失败!');
			},
			complete: function () {
			}
		});
		$('#main1')[0].reset();
		$('#mainId').val('');
		$('.close').click();
}

//获取数据的方法
function getDataForAdd() {
	var params = {};
	params.ticketDate = $("#ticketDate").val();
	params.proposer = $("#proposer").val();
	params.sumMoney = $("#sumMoney").val();
	params.comments = $("#comments").val();
	return params;
}
